package com.deyun.smartairporthandset;

import android.Manifest;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.apkfuns.logutils.LogUtils;
import com.deyun.smartairporthandset.fragment.IdRegFragment;
import com.deyun.smartairporthandset.fragment.PhotoFragment;
import com.deyun.smartairporthandset.fragment.VisitFragment;
import com.yanzhenjie.permission.Action;
import com.yanzhenjie.permission.AndPermission;

import java.util.List;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.core.content.PermissionChecker;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {

    // 退出时间
    private long mExitTime;
    @BindView(R.id.rg_group)
    RadioGroup rg_group;

    public Fragment[] mFragments;
    // 点击下标
    private int mIndex;

    public String sectionName = "DEFAULT";

    // 点击事件
    @OnClick({R.id.rb_id, R.id.rb_photo, R.id.rb_visit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rb_id:
                setIndexSelected(0);
                VisitFragment visitFragment_1 = (VisitFragment) mFragments[2];
                visitFragment_1.closeFaceReg();
                // 设置区域名称
                IdRegFragment idRegFragments_3 = (IdRegFragment) mFragments[0];
                idRegFragments_3.setSectionName(sectionName);
                break;
            case R.id.rb_photo:
                IdRegFragment idRegFragments_1 = (IdRegFragment) mFragments[0];
                idRegFragments_1.closeFaceReg();
                VisitFragment visitFragment_2 = (VisitFragment) mFragments[2];
                visitFragment_2.closeFaceReg();
                setIndexSelected(1);
                break;
            case R.id.rb_visit:
                // 关闭人脸识别
                IdRegFragment idRegFragments_2 = (IdRegFragment) mFragments[0];
                idRegFragments_2.closeFaceReg();
                // 设置区域名称
                VisitFragment visitFragment = (VisitFragment) mFragments[2];
                visitFragment.setSectionName(sectionName);
                setIndexSelected(2);
                break;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getSupportActionBar() != null) {
            getSupportActionBar().hide();
        }
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        rg_group.check(R.id.rb_id);
        initFragments();
    }

    // 设置显示指定的fragment
    public void setIndexSelected(int index) {
        if (mIndex == index) {
            return;
        }
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction ft = fragmentManager.beginTransaction();
        //隐藏
        ft.hide(mFragments[mIndex]);
        //判断是否添加
        if (!mFragments[index].isAdded()) {
            ft.add(R.id.fl_content, mFragments[index]).show(mFragments[index]);
        } else {
            ft.show(mFragments[index]);
        }
        // 异步防止频繁操作崩溃
        fragmentManager.executePendingTransactions();
        ft.commit();
        //再次赋值
        mIndex = index;
    }

    // 初始化fragment
    private void initFragments() {
        IdRegFragment idRegFragment = new IdRegFragment();
        PhotoFragment photoFragment = new PhotoFragment();
        VisitFragment visitFragment = new VisitFragment();
        mFragments = new Fragment[]{idRegFragment, photoFragment, visitFragment,};
        FragmentTransaction ft =
                getSupportFragmentManager().beginTransaction();
        ft.add(R.id.fl_content, idRegFragment).commit();
        setIndexSelected(0);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (checkPermission()) {

        } else {
            initPermission();
        }
    }

    // 检测权限
    public boolean checkPermission() {
        if (android.os.Build.VERSION.SDK_INT >= 23 && ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PermissionChecker.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PermissionChecker.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PermissionChecker.PERMISSION_GRANTED) {
            return false;
        } else {
            return true;
        }
    }

    // 申请权限
    public void initPermission() {
        // 申请权限。
        AndPermission.with(this)
                .permission(Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE).onGranted(new Action() {
            @Override
            public void onAction(List<String> permissions) {
                LogUtils.d("点击了同意");
            }
        }).onDenied(new Action() {
            @Override
            public void onAction(List<String> permissions) {
                if (checkPermission()) {
                    LogUtils.d("点击了拒绝同意");
                } else {
                    Toast.makeText(getApplicationContext(), "权限被禁止，人脸识别功能无法使用", Toast.LENGTH_SHORT).show();
                }
            }
        }).start();
    }

    // APP退出管理
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                if ((System.currentTimeMillis() - mExitTime) > 2000) {
                    Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
                    mExitTime = System.currentTimeMillis();
                } else {
                    finish();
                }
                break;
            default:
                return super.onKeyDown(keyCode, event);
        }
        return true;
    }
}